package model.semantics;

import model.State;

public class WhileNode extends Node<Integer> {

    public Node<Integer> myCondition;
    public Node<Integer> myBody;

    public WhileNode(Node<Integer> condition, Node<Integer> body) {
        myCondition = condition;
        myBody = body;
    }

    @Override
    public Integer evaluate(State<Integer> state) {
        int last = 0;
        while (myCondition.evaluate(state) > 0) {
            last = myBody.evaluate(state);
        }
        return last;
    }

    @Override
    public String toString() {
        return String.format("(while %s %s)", myCondition.toString(),
                myBody.toString());
    }

}
